import CryptoJS from "crypto-js";
function getKey() {
  // 真正的key
  const key = process.env["ENCODE_KEY"];
  return CryptoJS.enc.Utf8.parse(key);
}

function getIv() {
  // 真正的iv
  const iv = process.env["ENCODE_IV"];
  return CryptoJS.enc.Utf8.parse(iv);
}

export function encode(data) {
  var ciphertext = CryptoJS.AES.encrypt(data, getKey(), {
    iv: getIv(),
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  });
  return ciphertext.toString();
}

export function decode(data) {
  var bytes = CryptoJS.AES.decrypt(data.toString(), getKey(), {
    iv: getIv(),
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  });

  return bytes.toString(CryptoJS.enc.Utf8);
}
